package org.wpahs.ahn.rssfeed;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.util.Calendar;

/**
 * RSSCastReceiver
 * Created by dgambone on 01/12/2015.
 */
public class RSSCastReceiver extends BroadcastReceiver {
    //private AlarmManager alarmMgr;
    //private PendingIntent alarmIntent;
    private Context mContext = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            if(!getAutoPoll()){
                return;
            }
            RSSCastReceiver.SetupPolling(context,getAutoPollInMinutes());
        }else if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){
            // check to see if user wants to only use WIFI
            // if so.  Kill any running tasks
            CancelWebTasks(context);
        }
    }

    public static void ClearPolling(Context context){
        try {
            AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0,
                    new Intent(context, RSSAlarmReceiver.class),
                    PendingIntent.FLAG_NO_CREATE);

            if (alarmIntent != null) {
                alarmMgr.cancel(alarmIntent);
            }
            Log.i("RSSCastReceiver", "Cleared Polling");
        }catch (Exception ex){
            Log.e("RSSCastReceiver", ex.getMessage());
        }
    }


    private boolean getAutoPoll(){
        SharedPreferences prefs=
                mContext.getSharedPreferences(
                        mContext.getString(
                                R.string.preferences_gambonereader),
                        Context.MODE_PRIVATE);
        return prefs.getBoolean(
                mContext.getString(R.string.preferences_enableAuto),
                false);

    }
    private int getAutoPollInMinutes(){
        SharedPreferences prefs=
                mContext.getSharedPreferences(
                        mContext.getString(
                                R.string.preferences_gambonereader),
                        Context.MODE_PRIVATE);
        return prefs.getInt(
                mContext.getString(R.string.preference_autoPollInMinutes),
                60);

    }



    public static void SetupPolling(Context context,int minutesToWait){


        boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
                new Intent(context, RSSGetService.class),
                PendingIntent.FLAG_NO_CREATE) != null);

        if (!alarmUp)
        {
            AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, RSSAlarmReceiver.class);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

            // Set the alarm to start at 8:30 a.m.
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());

            // kill it if it exists
            try {
                alarmMgr.cancel(alarmIntent);
            } catch (Exception e) {
                Log.i("RSSCastReceiver", "AlarmManager update was not canceled. " + e.toString());
            }
            // setRepeating() lets you specify a precise custom interval--in this case,
            // 30 minutes.
            alarmMgr.setInexactRepeating(
                    AlarmManager.RTC,
                    calendar.getTimeInMillis(),
                    1000 * 60 * minutesToWait,
                    alarmIntent
            );
            Log.i("RSSCastReceiver", "Alarm was set");
        }else{
            Log.i("RSSCastReceiver", "Alarm is already active");
        }


    }

    private void CancelWebTasks(Context context){
        if(RSSAlarmReceiver.isMyServiceRunning(context,RSSGetService.class)){
            context.stopService(new Intent(context,RSSGetService.class));
        }

    }
}
